package com.yanqu.road.server.cmd.activity.suitang.cross;

import com.yanqu.road.entity.activity.suitang.config.SuiTangConfigSync;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.log.LogSuiTangAutoSpeed;
import com.yanqu.road.entity.task.args.ConsumeItemArgs;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.suitang.SuiTang;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.suitang.SuiTangMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.List;

@Cmd(code = GameProtocol.S_SUI_TANG_NOTIFY_CONDITION_FROM_CROSS, desc = "")
public class SuiTangNotifyConditionFromCrossCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        SuiTang.CrossSuiTangNotifyConditionRespMsg respMsg = SuiTang.CrossSuiTangNotifyConditionRespMsg.parseFrom(packet.getBytes());
        SuiTangConfigSync config = SuiTangMgr.getConfig();
        if (config == null) {
            return;
        }
        List<String> paramList = StringUtils.stringToStringList(respMsg.getParam(), "#");
        // 通知变更
        if (respMsg.getConditionType() == eGamePlayerEventType.ConsumeItemRank.getValue()) {
            // 道具消耗不在线的玩家先load出来
            if (player == null) {
                player = GamePlayerMgr.getPlayer(respMsg.getUserId());
            }
            if (player != null) {
                long consumeItemNum = 0;
                if (!paramList.isEmpty() && paramList.get(0).equals(String.valueOf(GameConfig.GAME_MONEY_INGOTS))) {
                    // 加速元宝消耗
                    consumeItemNum = config.getParamConfig().getAutoSettingIngotsNum() * respMsg.getValue();

                    // 顺便记一下记录
                    try {
                        AutoLogMgr.add(new LogSuiTangAutoSpeed(
                                respMsg.getActivityId(),
                                respMsg.getUserId(),
                                Integer.parseInt(paramList.get(1)),
                                Integer.parseInt(paramList.get(2)),
                                Integer.parseInt(paramList.get(3)),
                                Integer.parseInt(paramList.get(4)),
                                Integer.parseInt(paramList.get(5)),
                                respMsg.getValue(),
                                consumeItemNum
                        ));
                    } catch (Exception e) {
                        log.error("add LogSuiTangAutoSpeed logger error ,userId:{},ingotsCost:{}", respMsg.getUserId(), consumeItemNum);
                    }
                    player.notifyListener(respMsg.getConditionType(), new ConsumeItemArgs(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(consumeItemNum)));
                } else {
                    // 其他的道具消耗暂不处理
                }
            }
        } else {
            // 其他的
            if (player != null) {
                player.notifyListener(respMsg.getConditionType(), respMsg.getValue());
            } else {
                // 空的先不处理，这里会频繁读库，先不要了，上线再拉，目前只有战功相关的
            }
        }
    }

    @Override
    public void sendData(GamePlayer player) {

    }

}
